<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='./vue.global.js'></script>
</head>

<body>
    <div id='app'>
        <todo-list></todo-list>
    </div>

    <script>
        const app = Vue.createApp({})

        app.component('todo-list', {
            data() {
                return {
                    todos: ['Feed a cat', 'Buy tickets']
                }
            },

            // provide: {
            //     user: 'John Doe'
            // },

            // 要访问组件实例 property，我们需要将 provide 转换为返回对象的函数：
            provide() {
                return {
                    user: 'John Doe',
                    // todoLength: this.todos.length,
                    todoLength: Vue.computed(() => this.todos.length),
                }
            },

            template: `
            <div>
                {{ todos.length }}
                <son></son>
            </div>
            `
        })

        app.component('son', {
            inject: ['user','todoLength'],
            created() {
                console.log(`Injected property: ${this.user}`) // > 注入的 property: John Doe
                console.log(`Injected property: ${this.todoLength}`) // > 注入的 property: John Doe
            },
            template:`son:{{todoLength}}`
        })

        const vm = app.mount('#app')
    </script>
</body>

</html>